<template>
  <div class="com-container">
    <div class="title" :style="titleStyle">
      <span>{{'▎' + showTitle }}</span>
      <span class="iconfont title-icon" @click="selectShow = !selectShow" :style="titleStyle"
        >&#xe6eb;</span
      >
      <div class="selectOption" v-show="selectShow" :style="marginStyle">
        <div
          v-for="item in selectOption"
          :key="item.key"
          @click="hendleChois(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="ref_trend"></div>
  </div>
</template>

<script>
import { getMerchants } from '@/API/api.js'
export default {
  name: 'Trend',
  data() {
    return {
      // echarts 实例对象
      myChart: null,
      // 数据
      allData: null,
      // 控制下拉菜单显示隐藏
      selectShow: false,
      // 标题
      choisType: 'map',
      // 标题文字大小
      titleFontSize: 0
    }
  },
  // 页面渲染完毕后
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdater)
    this.screenAdater()
  },
  // 组件销毁后
  destroyed() {
    // 删除窗口大小变化监听事件
    window.removeEventListener('resize', this.screenAdater)
  },
  computed: {
    selectOption() {
      if (!this.allData) return []
      return this.allData.type.filter((item) => {
        return item.key !== this.choisType
      })
    },
    showTitle() {
      if (!this.allData) return ''
      return this.allData[this.choisType].title
    },
    titleStyle() {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize - 18 + 'px'
      }
    }
  },
  methods: {
    // 初始化图表
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.ref_trend, 'chalk')
      const initOption = {
        grid: {
          top: '35%',
          right: '3%',
          bootom: '1%',
          left: '4%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        legend: {
          left: '5%',
          top: '15%',
          icon: 'circle'
        },
        yAxis: {
          type: 'value'
        },
        tooltip: {
          trigger: 'axis'
        }
      }
      this.myChart.setOption(initOption)
    },
    // 获取数据
    async getData() {
      const { data: res } = await getMerchants()
      console.log(res)
      this.allData = res
      this.updateChart()
    },
    // 更新图表
    updateChart() {
      // 处理数据
      // 半透明的颜色值
      const colorArr1 = [
        'rgba(11, 168, 44, 0.5)',
        'rgba(44, 110, 255, 0.5)',
        'rgba(22, 242, 217, 0.5)',
        'rgba(254, 33, 30, 0.5)',
        'rgba(250, 105, 0, 0.5)'
      ]
      // 全透明的颜色值
      const colorArr2 = [
        'rgba(11, 168, 44, 0)',
        'rgba(44, 110, 255, 0)',
        'rgba(22, 242, 217, 0)',
        'rgba(254, 33, 30, 0)',
        'rgba(250, 105, 0, 0)'
      ]
      // 类目轴数据
      const xAxisData = this.allData.common.month
      // 数值轴数据
      const yAxisData = this.allData[this.choisType].data.map((item, index) => {
        return {
          name: item.name,
          type: 'line',
          stack: 'all',
          smooth: true,
          data: item.data,
          areaStyle: {
            color: {
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                { offset: 0, color: colorArr1[index] },
                { offset: 1, color: colorArr2[index] }
              ]
            }
          }
        }
      })
      // 图例的数据
      const legendValue = this.allData[this.choisType].data.map((item) => {
        return item.name
      })
      const dataOption = {
        xAxis: {
          data: xAxisData
        },
        series: yAxisData,
        legend: {
          data: legendValue
        }
      }
      this.myChart.setOption(dataOption)
    },
    // 分辨率适配
    screenAdater() {
      this.titleFontSize = (this.$refs.ref_trend.offsetWidth / 100) * 3.6
      const adaterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2
          }
        }
      }
      this.myChart.setOption(adaterOption)
      this.myChart.resize()
    },
    // 切换标题
    hendleChois(choisType) {
      this.choisType = choisType
      this.updateChart()
      this.selectShow = false
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 999;
  color: #fff;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .selectOption {
    background-color: #222733;
  }
}
</style>
